﻿package net.wangit.view.unit.component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.wangit.MapList;
import net.wangit.Row;
import net.wangit.context.ActionContext;
import net.wangit.framework.util.UnitDataUtil;
import net.wangit.jdbc.DB;
import net.wangit.jdbc.DBFactory;
import net.wangit.jdbc.table.Column;
import net.wangit.jdbc.table.TableMeta;
import net.wangit.jdbc.table.TableMetaFactory;
import net.wangit.util.Checker;
import net.wangit.view.template.Template;
import net.wangit.view.template.TemplateFactory;
import net.wangit.view.unit.UnitComponent;
import net.wangit.view.unit.UnitFactory;


public class FormNavigatorUtil {

	private static final String TEMPLATE = "nav.form.html";
	private static final String PREVIOUS = "previous";
	private static final String NEXT = "next";
	private static final String DISABLED = " class=\"ND\"";

	public FormNavigatorUtil() {
	}

	public static String getNavigatorHtml(ActionContext ac, FormUnit fu)
			throws Exception {
		String listUnitId = fu.getListUnitId();
		String tableName = fu.getTableName();
		if (Checker.isEmpty(listUnitId) || Checker.isEmpty(tableName))
			return null;
		String domain = fu.getDomain();
		Row values = fu.getData().getRow(0);
		TableMeta table = TableMetaFactory.getTableMeta(domain, tableName);
		List columns = table.getPrimaryKey().getColumns();
		MapList data = getListData(ac, domain, listUnitId);
		int i = 0;
		for (i = 0; i < data.size(); i++)
			if (equal(columns, values, data.getRow(i)))
				break;

		Map attributes = new HashMap();
		String formUnitId = fu.getId();
		String url = (new StringBuilder("/")).append(domain).append("/")
				.append(formUnitId).append(".do?").toString();
		attributes.put("previous", getParameters(url, columns, data
				.getRow(i - 1), formUnitId));
		attributes.put("next", getParameters(url, columns, data.getRow(i + 1),
				formUnitId));
		Template t = TemplateFactory.getTemplate("nav.form.html");
		return t.combine(attributes);
	}

	private static MapList getListData(ActionContext ac, String domain,
			String listUnitId) throws Exception {
		UnitComponent lu = UnitFactory.getUnitWithoutClone(domain, listUnitId);
		DB db = DBFactory.getDB(lu.getDatasource());
		String sql = UnitDataUtil.getSql(ac, lu, db);
		return db.query(sql);
	}

	private static boolean equal(List columns, Row values, Row row) {
		for (int i = 0; i < columns.size(); i++) {
			String columnName = ((Column) columns.get(i)).getName();
			if (!values.get(columnName).equals(row.get(columnName)))
				return false;
		}

		return true;
	}

	private static String getParameters(String url, List columns, Row row,
			String formUnitId) {
		if (row == null)
			return " class=\"ND\"";
		StringBuffer parameters = new StringBuffer(" href=\"");
		parameters.append(url);
		for (int i = 0; i < columns.size(); i++) {
			if (i > 0)
				parameters.append("&");
			Column column = (Column) columns.get(i);
			String columnName = column.getName();
			parameters.append(formUnitId).append(".").append(columnName)
					.append("=").append(row.get(columnName));
		}

		return parameters.append("\"").toString();
	}
}
